package com.qf.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 策略模块  监听 接口模块发送过来的消息
 *
 * @author lixu
 */
@Component
public class RabbitMqListener {


    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private FilterChainManager filterChainManager;

    /**
     * 发短信的 对象消息
     *
     *    交换机(pre_send_sms_topic_exchange)  ---#--》  队列(pre_send_sms_topic)
     *
     *    准备使用注解的方式 创建交换机  创建队列  及交接机与队列之间绑定关系
     *
     */
    @RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(name = RabbitMQConstants.TOPIC_PRE_SEND + "_exchange",type = "topic"),//交换机
            value = @Queue(name = RabbitMQConstants.TOPIC_PRE_SEND,durable = "true",autoDelete = "false"),//队列
            //key = {"#.lazy.*","*.orange.rabbit"}
            key = {RabbitMQConstants.BINDING}
    )})
    public void receiverMessageSendSms(StandardSubmit standardSubmit, Channel channel, Message message) {
        try {
            System.out.println("策略模块接收消息:" + objectMapper.writeValueAsString(standardSubmit));
            //开始执行我们策略
            filterChainManager.executeFilter(standardSubmit);
            //手动应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
